package com.iwdnb.gkgz.infrastructure.dao.repo.impl;

import java.util.List;

import cn.hutool.core.util.RandomUtil;
import com.iwdnb.bmnf.common.model.Page;
import com.iwdnb.gkgz.common.model.dto.prediction.StockPredictionParam;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockPredictionParamRepository;
import com.iwdnb.wwzy.dao.MetaDataDAO;
import com.iwdnb.wwzy.model.query.MetaQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STOCK_PREDICTION_PARAM;

@Component
public class StockPredictionParamRepositoryImpl implements StockPredictionParamRepository {
    @Autowired
    private MetaDataDAO metaDataDAO;
    private static final String OBJECT_CODE = OBJECT_CODE_STOCK_PREDICTION_PARAM;

    @Override
    public void insert(List<StockPredictionParam> params) {
        boolean end = false;
        for (int i = 0; i < 1000; i++) {
            int startIndex = 1000 * i;
            int endIndex = 1000 * (i + 1);
            if (endIndex >= params.size() - 1) {
                endIndex = params.size() - 1;
                end = true;
            }
            List<StockPredictionParam> subList = params.subList(startIndex, endIndex);
            metaDataDAO.insert(OBJECT_CODE, subList, false, false);
            if (end) {
                break;
            }
        }
    }

    @Override
    public void insert(StockPredictionParam param) {
        metaDataDAO.insert(OBJECT_CODE, param);
    }

    @Override
    public List<StockPredictionParam> queryNotPredictionParams(int begin,String sort) {
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("predictionFlag", "n");
        metaQuery.setPage(new Page(begin, 100));
        if (StringUtils.equals(sort, "asc")) {
            metaQuery.setOrderASC("id");
        } else {
            metaQuery.setOrderDESC("id");
        }
        return metaDataDAO.queryListByCondition(OBJECT_CODE, metaQuery,
            StockPredictionParam.class);
    }

    @Override
    public void update(StockPredictionParam param) {
        metaDataDAO.update(OBJECT_CODE, param);
    }

    @Override
    public StockPredictionParam getParamByUUID(String uuid) {
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("uuid", uuid);
        return metaDataDAO.get(OBJECT_CODE, metaQuery, StockPredictionParam.class);
    }

    @Override
    public void update(List<StockPredictionParam> stockPredictionParams) {
        metaDataDAO.update(OBJECT_CODE, stockPredictionParams);
    }
}
